﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using LCA.Community.DAL.Test.ModelFactories;
using Microsoft.Practices.Unity;

namespace LCA.Community.DAL.Test
{
    
    
    /// <summary>
    ///This is a test class for AdminsRepositoryTest and is intended
    ///to contain all AdminsRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AdminsRepositoryTest : BaseTestClass
    {
        #region Fields
        private List<User> _users;
        private UsersFactory _usersFactory = new UsersFactory();
        private AdminsFactory _adminsFactory = new AdminsFactory();
        private List<Admin> _admins;
        #endregion

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users = _usersFactory.CreateList(10);
            _users.ForEach(user => usersRepository.Add(user));

            var adminsRepository = _unityContainer.Resolve<IAdminsRepository>();
            _admins = new List<Admin>();
            Admin admin;

            admin = _adminsFactory.CreateEntity(_users[0].RowKey);
            _admins.Add(admin);
            adminsRepository.Add(admin);

            admin = _adminsFactory.CreateEntity(_users[1].RowKey);
            _admins.Add(admin);
            adminsRepository.Add(admin);

            admin = _adminsFactory.CreateEntity(_users[2].RowKey);
            _admins.Add(admin);
            adminsRepository.Add(admin);
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            var adminsRepository = _unityContainer.Resolve<IAdminsRepository>();
            usersRepository.DeleteTestEntities();
            adminsRepository.DeleteTestEntities();
        }

        /// <summary>
        ///A test for IsAdmin
        ///</summary>
        [TestMethod()]
        public void IsAdminTest()
        {
            var target = _unityContainer.Resolve<IAdminsRepository>();
            string userID = _users[0].RowKey;
            bool actual;

            actual = target.IsAdmin(userID);
            Assert.IsTrue(actual);

            userID = _users[3].RowKey;
            actual = target.IsAdmin(userID);
            Assert.IsFalse(actual);
        }
    }
}
